<%@ page language="java" import="up7.*" pageEncoding="UTF-8"%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%@	page import="com.google.gson.Gson" %>
<%@ page import="up7.model.*" %>
<%@ page import="up7.biz.*" %>
<%@ page import="up7.utils.*" %>
<%@ page import="up7.database.sql.SqlFile" %>
<%@	page import="org.apache.commons.lang.StringUtils" %>
<%@ page import="java.net.URLDecoder" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="net.sf.json.JSONObject" %>
<%@ page import="up7.sql.SqlWhereMerge" %>
<%@ page import="up7.sql.SqlExec" %>
<%@ page import="net.sf.json.JSONArray" %>
<%@ page import="filemgr.PageFileMgr" %>
<%@ page import="java.io.IOException" %>
<%@ page import="java.io.UnsupportedEncodingException" %>
<%
    out.clear();

    WebBase web = new WebBase(pageContext);

    String id       = web.queryString("id");
    String pid      = web.queryString("pid");
    String pidRoot  = web.queryString("pidRoot");
    if( StringUtils.isBlank(pidRoot)) pidRoot = pid;//父目录是根目录
    String uid      = web.queryString("uid");
    String fCount   = web.queryString("filesCount");
    String lenLoc   = web.queryString("lenLoc");
    String sizeLoc  = web.queryString("sizeLoc");
    String pathLoc  = web.queryString("pathLoc");
    String pathRel  = web.queryString("pathRel");
    pathRel 		= PathTool.url_decode(pathRel);
    pathLoc			= PathTool.url_decode(pathLoc);
    String callback = web.queryString("callback");//jsonp


    //参数为空
    if (StringUtils.isBlank(id)
            || StringUtils.isBlank(uid)
            || StringUtils.isBlank(pathLoc))
    {
        web.toContent(callback + "({\"value\":null})");
        return;
    }

    FileInf fileSvr = new FileInf();
    fileSvr.id      = id;
    fileSvr.pid     = pid;
    fileSvr.pidRoot = pidRoot;
    fileSvr.fdChild = false;
    fileSvr.fdTask  = true;
    fileSvr.uid     = uid;
    fileSvr.nameLoc = PathTool.getName(pathLoc);
    fileSvr.pathLoc = pathLoc;
    fileSvr.pathRel = PathTool.combine(pathRel, fileSvr.nameLoc);
    fileSvr.lenLoc  = Long.parseLong(lenLoc);
    fileSvr.fileCount = Integer.parseInt(fCount);
    fileSvr.sizeLoc = sizeLoc;
    fileSvr.deleted = false;
    fileSvr.nameSvr = fileSvr.nameLoc;

    //检查同名目录
		/*DbFolder df = new DbFolder();
		if (df.exist_same_folder(fileSvr.nameLoc, pid))
		{
			JSONObject o = new JSONObject();
			o.put("value","");
			o.put("ret", false);
			o.put("code", "102");
		    String js = callback + String.format("(%s)", o.toString());
		    web.toContent(js);
		    return;
		}*/

    //生成存储路径
    PathGuidBuilder pb = new PathGuidBuilder();
    fileSvr.pathSvr = pb.genFolder(fileSvr.uid, fileSvr.id, fileSvr.nameLoc);
    fileSvr.pathSvr = fileSvr.pathSvr.replace("\\","/");
    PathTool.mkdir(fileSvr.pathSvr);

  	//创建层级结构
    FolderSchema fs = new FolderSchema();
    fs.create(fileSvr);
    
  	//添加到根目录
    if(StringUtils.equals(pathRel,"/")) SqlFile.build().Add(fileSvr);
    //添加到子目录
    else DbFolder.build().add(fileSvr);

    up7_biz_event.folder_create(fileSvr);

    Gson g = new Gson();
    String json = g.toJson(fileSvr);
    json        = PathTool.url_encode(json);

    JSONObject ret = new JSONObject();
    ret.put("value",json);
    ret.put("ret",true);
    json = callback + String.format("(%s)",ret.toString());//返回jsonp格式数据。
    out.write(json);
%>